// 删除sessionStorage
export const delSessionStorage = () => {
  sessionStorage.removeItem('baseInfoVO');
  sessionStorage.removeItem('staticTable');
}

// 请求内容类型按照application/x-www-form-urlencoded类型请求
export const reverseContentType = (obj) => {
  const params = new URLSearchParams();
  for (const key in obj) {
    params.append(key, obj[key]);
  }
  return params;
};

// 说明：文件下载接口，返回的是数据流，因此接口中必须使用{responseType:arraybuffer}
// `responseType` 表示服务器响应的数据类型，可以是 "arraybuffer", "blob", "document", "json", "text", "stream"
export const downloadFile = (httpFun: Function, params: any, fileName?: string) => {
  httpFun(params).then((res: any) => {
    const blob = new Blob([res], {
      type: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    });
    let href = (window.URL || window.webkitURL).createObjectURL(blob);
    if (window.navigator.msSaveOrOpenBlob) {
      // IE10+下载
      navigator.msSaveBlob(blob, fileName + '.xlsx');
    } else {
      // 非IE下载
      let downloadElement = document.createElement('a');
      downloadElement.href = href;
      downloadElement.download = fileName; // 下载后文件名
      document.body.appendChild(downloadElement);
      downloadElement.click(); // 点击下载
      document.body.removeChild(downloadElement); // 下载完成移除元素
      window.URL.revokeObjectURL(href); // 释放掉blob对象
    }
  });
};

// 数组以逗号隔开，形成字符串
export function joinStr(value: string[]) {
  let str = ''
  if (value && value.length) {
    str = value.join(',')
  } else {
    str = ''
  }
  return str
}

// 字符串分割成数组
export function splitStr(value: string) {
  let arr: any = []
  if (value && value.length) {
    arr = value.split(',')
  } else {
    arr = ''
  }
  return arr
}

// 对象转数组
export function objectToArrat(obj:any) {
  if (Object.keys(obj).length === 0) return
  const arr = []
  for (const key in obj) {
    arr.push({ key: obj[key] })
  }
  return arr
}
